<template>
  <j-modal
    :title="'设置定时报告'"
    :width="950"
    :visible="visible"
    :maskClosable="false"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    :confirmLoading="loading"
  >
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
    </template>
    <a-spin :spinning="loading">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24"> </a-row>
          </a-form>
        </div>
        <!-- 查询区域-END -->
          <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            :scroll="{ x: true }"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap"
            @change="handleTableChange"
          >
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>
            <span slot="parameterRender" slot-scope="text">
              {{reportItem(text,'reportName')}}
            </span>
            <span slot="cronExpressionRender" slot-scope="text">
              {{dateFormt(text)}}
            </span> 
            <!-- 状态渲染模板 -->
            <template slot="customRenderStatus" slot-scope="status">
              <a-tag v-if="status==0" color="green">已启动</a-tag>
              <a-tag v-if="status==-1" color="orange">已暂停</a-tag>
            </template> 

              <span slot="action" slot-scope="text, record">
                <a @click="resumeJob(record)" v-if="record.status==-1">启动</a>
                <a @click="pauseJob(record)" v-if="record.status==0">停止</a>

                <a-divider type="vertical" />
                <a-dropdown>
                  <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                  <a-menu slot="overlay">
                    <a-menu-item><a @click="executeImmediately(record)">执行一次</a></a-menu-item>
                    <!-- <a-menu-item><a @click="handleEdit(record)">编辑</a></a-menu-item> -->
                    <a-menu-item>
                      <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                        <a>删除</a>
                      </a-popconfirm>
                    </a-menu-item>
                  </a-menu>
                </a-dropdown>
              </span>
              
          </a-table>
        </div>

        <report-cron-job-data-set-model ref="modalForm" @ok="modalFormOk"></report-cron-job-data-set-model>
      </a-card>
    </a-spin>
  </j-modal>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
import ReportCronJobDataSetModel from './ReportCronJobDataSetModel'

export default {
  name: 'ReportCronJobDataList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ReportCronJobDataSetModel,
  },
  data() {
    return {
      description: '报表定时任务数据设置管理页面',
      visible: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },  
          {
            title: '执行时间',
            align:"center",
            dataIndex: 'cronExpression',
            scopedSlots: {customRender: 'cronExpressionRender'},
          },
          {
            title: '报告名称',
            align:"center",
            width: 150,
            dataIndex: 'parameter',
            scopedSlots: {customRender: 'parameterRender'},
          },
          {
            title: '描述',
            align:"center",
            width: 250,
            dataIndex: 'description',
            scopedSlots: {customRender: 'description'},
          },
          {
            title: '状态',
            align:"center",
            dataIndex: 'status',
            scopedSlots: { customRender: 'customRenderStatus' },
            filterMultiple: false,
            filters: [
              { text: '已启动', value: '0' },
              { text: '已暂停', value: '-1' },
            ]
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:180,
            scopedSlots: { customRender: 'action' },
          }
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align:"center",
        //   fixed:"right",
        //   width:147,
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: "/sys/quartzJob/list",
        delete: "/sys/quartzJob/delete",
        deleteBatch: "/sys/quartzJob/deleteBatch",
        pause: "/sys/quartzJob/pause",
        resume: "/sys/quartzJob/resume",
        exportXlsUrl: "sys/quartzJob/exportXls",
        importExcelUrl: "sys/quartzJob/importExcel",
        execute: "sys/quartzJob/execute"
      },
      dictOptions: {},
      superFieldList: [],
      loading: false,
      queryParam: {
        jobClassName: 'org.jeecg.modules.userReports.job.userReportsJob', //任务类名：报告定时生成任务
      },
      cronExpressionList:[
        {"key":"0 0 1 * * ? *","value":"凌晨1点"},
        {"key":"0 0 2 * * ? *","value":"凌晨2点"},
        {"key":"0 0 3 * * ? *","value":"凌晨3点"},
        {"key":"0 0 4 * * ? *","value":"凌晨4点"}, 
      ] 
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    reportItem(text,key){
      var jsonData= eval("(" + text + ")")
      return jsonData[key]
    },
    dateFormt(text){
       var obj = this.cronExpressionList.filter(function(x) { return x.key == text})[0]
       return obj['value'];
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'paramId', text: '定时任务设置id', dictCode: '' })
      fieldList.push({ type: 'int', value: 'reportId', text: '报表id', dictCode: '' })
      fieldList.push({ type: 'string', value: 'reportName', text: '报表名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'fileUrl', text: '文件', dictCode: '' })
      this.superFieldList = fieldList
    }, 
       pauseJob: function(record){
        var that = this;
        //暂停定时任务
        this.$confirm({
          title:"确认暂停",
          content:"是否暂停选中任务?",
          onOk: function(){
            getAction(that.url.pause,{id:record.id}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              }else{
                that.$message.warning(res.message);
              }
            });
          }
        });

      },
      resumeJob: function(record){
        var that = this;
        //恢复定时任务
        this.$confirm({
          title:"确认启动",
          content:"是否启动选中任务?",
          onOk: function(){
            getAction(that.url.resume,{id:record.id}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              }else{
                that.$message.warning(res.message);
              }
            });
          }
        });
      },
      executeImmediately(record){
        var that = this;
        //立即执行定时任务
        this.$confirm({
          title:"确认提示",
          content:"是否立即执行任务?",
          onOk: function(){
            getAction(that.url.execute,{id:record.id}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              }else{
                that.$message.warning(res.message);
              }
            });
          }
        });
      },
    show() {
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    }, 
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>